import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/attachment_bean.dart';
import 'package:app/src/http/bean/wroks/contract_bean.dart';
import 'package:app/src/http/bean/wroks/customers_copyInfo.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../../../http/url.dart';

///附件
class UiAttachment {
  static StateSetter? _setState = null;

  ///data 附件
  ///isShowEmpty 信息为空时，是否显示信息为空。true 显示信息为空，false 什么都不显示。
  ///isDel 是否允许删除附件
  ///titleRed 标题是否为红色
  static Widget build(BuildContext context, List<String>? data,
      {bool isShowEmpty = false,
      String? title = null,
      bool isDel = false,
      bool titleRed = false}) {
    return StatefulBuilder(
      builder: (context, setState) {
        _setState = setState;
        if ((!isShowEmpty) && StringUtils.isEmpty(data)) {
          return AppUi.getEmptyView();
        }
        List<Widget> children = [];
        if (StringUtils.isEmpty(title)) {
          title = AppStrings.of(context).Attachment;
        }
        children.add(UiBase.title(context, title, titleRed: titleRed)); //标题
        if (StringUtils.isEmpty(data)) {
          children.add(UiBase.itemWithSingle(
              context, AppStrings.of(context).AttachmentEmpty, "")); //当前附件为空!
        } else {
          children.add(_items(context, data, isDel)); //数据
        }
        var column = UiBase.column(context, children);
        var card = UiCard.build(context, column);
        return card;
      },
    );
  }

  static Widget _items(BuildContext context, List<String>? data, bool isDel) {
    var listView = ListView.builder(
        shrinkWrap: true, //范围内进行包裹（内容多高ListView就多高）
        physics: NeverScrollableScrollPhysics(), //禁止滚动
        itemCount: data?.length ?? 0,
        itemBuilder: (context, index) {
          return Container(
            child: Row(
              children:
                  _item(context, data?[index], index + 1, index, data, isDel),
            ),
            margin: EdgeInsets.only(top: 8),
          );
        });
    return listView;
  }

  static List<Widget> _item(BuildContext context, String? srcData, int serial,
      int index, List<String>? datas, bool isDel) {
    var data = srcData;
    if (data != null && data.contains(",")) {
      data = data.split(",")[0];
    }
    List<Widget> children = [];
    var serialView = _text(context, serial.toString() + "、"); //序号
    var fileNameView =
        _text(context, FileUtils.getFileName(data), isBlue: true); //文件名
    Expanded expanded = Expanded(
        child: Row(
      children: [
        SizedBox(
          child: serialView,
          width: 30,
        ),
        InkWell(
          child: fileNameView,
          onTap: () {
            //data?.open(context); //打开文件
            var FilePatch = data;
            //http:/localhost:30929/UploadFile/PurchaseFile/20220601403791/001.jpeg
            //http:/test.bwg2017.com/UploadFile/PurchaseFile/20230512849967/下载.jpg
            // print("文件路径：$FilePatch");
            FileUtils.openFile(context, Url.getFileUrl(FilePatch));
          },
        )
      ],
    ));
    children.add(expanded);
    //children.add(_text(context, data?.StatusDescription)); //描述
    if (isDel) {
      children.add(Container(
        child: InkWell(
          child: Icon(Icons.delete),
          onTap: () {
            //data?.share(context); //文件分享
            AppDialog.showDialogWithAlert(context,
                    content: AppStrings.of(context).confirmDelete)
                ?.then((value) {
              if (value ?? false) {
                _setState?.call(() {
                  if (datas != null && datas!.length > index) {
                    datas?.removeAt(index); //fixme 删除
                  }
                });
              }
            });
          },
        ),
        margin: EdgeInsets.only(left: 10),
        width: 50,
      )); //删除
    }
    // children.add(Container(
    //   child: InkWell(
    //     child: Icon(Icons.share),
    //     onTap: () {
    //       //data?.share(context); //文件分享
    //       var FilePatch = data;
    //       FileUtils.shareFile(context, Url.getFileUrl(FilePatch));
    //     },
    //   ),
    //   margin: EdgeInsets.only(left: 10),
    //   width: 50,
    // )); //分享
    return children;
  }

  static Widget _text(BuildContext context, String? str,
      {bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    var text = Text(
      str?.trim() ?? "",
      style: style,
    );
    return Container(
      child: text,
      alignment: Alignment.centerLeft,
    );
  }
}
